//
//  CYWithdrawInputCell.m
//  cyjf
//
//  Created by tauCross on 2017/5/18.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYWithdrawInputCell.h"



@implementation CYWithdrawInputCellObject

@end



@interface CYWithdrawInputCell ()<UITextFieldDelegate>

@property(nonatomic, strong)UIImageView *inputTitleImageView;
@property(nonatomic, strong)UIView *bottomLine;

@property(nonatomic, strong)CYWithdrawInputCellObject *object;

@end


@implementation CYWithdrawInputCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.inputTitleImageView = [[UIImageView alloc] initWithImage:CYImage(@"ico_money_invest")];
        [self.contentView addSubview:self.inputTitleImageView];
        
        self.inputTextField = [[TCNoActionTextField alloc] init];
        self.inputTextField.font = [UIFont systemFontOfSize:16];
        self.inputTextField.backgroundColor = [UIColor clearColor];
        self.inputTextField.textColor = HEXCOLOR(@"444444");
        self.inputTextField.delegate = self;
        self.inputTextField.keyboardType = UIKeyboardTypeDecimalPad;
        [self.inputTextField addTarget:self action:@selector(onInputTextFieldEditingChanged) forControlEvents:UIControlEventEditingChanged];
        self.inputTextField.delegate = self;
        [self.contentView addSubview:self.inputTextField];
        
        self.bottomLine = [UIView cy_lineWithColor:HEXCOLOR(@"ECECEC")];
        [self.contentView addSubview:self.bottomLine];
    }
    return self;
}

+ (CGFloat)heightForObject:(id)object atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
{
    return 80;
}

- (BOOL)shouldUpdateCellWithObject:(id)object
{
    self.object = object;
    
    self.inputTextField.text = self.object.amount;
    self.inputTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"当日剩余可提%@次", self.object.limitCount ? : @"0"] attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16], NSForegroundColorAttributeName : HEXCOLOR(@"999999")}];
    
    [self setNeedsLayout]; 
    return YES;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.inputTitleImageView.centerY = self.contentView.height / 2;
    self.inputTitleImageView.left = 22;
    
    self.inputTextField.width = self.contentView.width - 20 - 48;
    self.inputTextField.height = self.contentView.height;
    self.inputTextField.left = 48;
    self.inputTextField.top = 0;
    
    self.bottomLine.width = self.contentView.width - 20;
    self.bottomLine.height = 1 / SCREEN_SCALE;
    self.bottomLine.bottom = self.contentView.height;
    self.bottomLine.left = 20;
}

#pragma mark - Control Events
- (void)onInputTextFieldEditingChanged
{
    self.object.amount = self.inputTextField.text;
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if(textField == self.inputTextField)
    {
        NSString *result = [textField.text stringByReplacingCharactersInRange:range withString:string];
        if(result.length == 0)
        {
            return YES;
        }
        NSString *regex = @"^\\-?([1-9]\\d*|0)(\\.\\d{0,2})?$";
        NSPredicate *predicte = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
        return [predicte evaluateWithObject:result];
    }
    return YES;
}

@end
